package com.zfp.sslserver;

import io.grpc.ManagedChannel;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
import org.junit.Before;

import javax.net.ssl.SSLException;
import java.io.File;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

public abstract class GrpcClient {

    private File certFile = new File("/home/zfp/demo/ssl-server-demo/src/main/resources/ssl/grpc_ca/gserver.cer");

    protected ManagedChannel channel;

    @Before
    public void init() throws SSLException {

        InetAddress inetAddress;
        try {

            inetAddress = InetAddress.getByName("localhost");
            channel = NettyChannelBuilder.forAddress(new InetSocketAddress(inetAddress, 9999))
                    .sslContext(GrpcSslContexts.forClient().trustManager(certFile).build())
                    .negotiationType(NegotiationType.TLS)
                    .build();

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}